package com.kongjian.manage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.kongjian.manage.common.R;
import com.kongjian.manage.model.dto.LoginSysUserDTO;
import com.kongjian.manage.model.dto.LoginVerificationSysUserDTO;
import com.kongjian.manage.service.ISysUserService;
import com.kongjian.manage.model.vo.SysUserVO;
import com.kongjian.manage.model.dto.UpdateSysUserDTO;
import com.kongjian.manage.model.dto.NewSysUserDTO;
import com.kongjian.manage.model.query.SysUserQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

/**
 * @author tangjunfeng
 * @Classname SysUserController.java
 * @Description 用户管理controller
 * @Date 2023-12-18
 * @Version V1.0
 */
@Api(tags = "数字孪生安全改造(用户管理)")
@RestController
@Validated
@RequestMapping("/api/v1/sys-user")
public class SysUserController {


    @Autowired
    private ISysUserService sysUserService;

    @ApiOperationSupport(order = 1)
    @GetMapping("page")
    @ApiOperation(value = "查询",hidden = true)
    public R<Page> queryPageSysUser(@ApiIgnore SysUserQuery query) throws Exception {
        return sysUserService.queryPageSysUser(query);
    }

    @ApiOperationSupport(order = 2)
    @GetMapping("count")
    @ApiOperation(value = "统计",hidden = true)
    public Integer countSysUser(SysUserQuery query) {
        return sysUserService.countSysUser(query);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据ID查询",hidden = true)
    @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", example = "1", required = true)
    @GetMapping("/{id}")
    public SysUserVO getByIdSysUser(@PathVariable("id") Long id) throws Exception {
        return sysUserService.getByIdSysUser(id);
    }



    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "更新",hidden = true)
    @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", example = "1", required = true)
    @PutMapping("/{id}")
    public R<String> modifySysUser(@PathVariable("id") Long id, @Validated @RequestBody UpdateSysUserDTO sysUserDTO) throws Exception {
        sysUserService.modifySysUser(id, sysUserDTO);
        return R.success("更新成功");
    }


    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "登录",hidden = true)
    @PostMapping("/login")
    public R<String> login(@RequestBody LoginSysUserDTO loginSysUserDTO) {
        sysUserService.login(loginSysUserDTO);
        return R.success("登录成功");
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "签名",hidden = true)
    @PostMapping("/signature")
    public R<String> signature(@RequestBody LoginSysUserDTO loginSysUserDTO) {
        sysUserService.signature(loginSysUserDTO);
        return R.success("签名成功");
    }



    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "注册")
    @PostMapping("")
    public R<String> addSysUser(@RequestBody @Validated NewSysUserDTO sysUserDTO) throws Exception {
        sysUserService.addSysUser(sysUserDTO);
        return R.success("注册成功");
    }


    @ApiOperationSupport(order = 7)
    @ApiOperation("第一步：签名")
    @PostMapping("/first-signature")
    public R<String> firstSignature(@RequestBody LoginSysUserDTO loginSysUserDTO) {
        sysUserService.firstSignature(loginSysUserDTO);
        return R.success("签名验签成功");
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation("第二步：验签")
    @PostMapping("/second-verification")
    public R<String> secondVerification(@RequestBody LoginVerificationSysUserDTO loginSysUserDTO, @ApiIgnore HttpSession session) {
        String token = sysUserService.secondVerification(loginSysUserDTO, session);
        return R.success(token);
    }




}
